package edu.utsa.cs.contactmanager.views;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import edu.utsa.cs.contactmanager.Contact;
import edu.utsa.cs.contactmanager.ContactManager;
import edu.utsa.cs.contactmanager.Group;
import edu.utsa.cs.contactmanager.OnTaskCompleted;
import edu.utsa.cs.contactmanager.R;

public class View_Groups extends ListActivity implements OnTaskCompleted {
	private List<Group> groups;
	private ArrayList<String> names;
	private HashMap<String, Group> hm;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);          
    }
    
    @Override
    public void onResume(){
    	ContactManager.open();
    	resetGroupsView();
    	super.onResume();
    };
    
    @Override
    public void onPause(){
    	ContactManager.close();
    	super.onPause();
    }
    
    private void resetGroupsView(){
        System.out.println("resetGroupsView called");
        groups = ContactManager.getGroups();
        hm = new HashMap<String, Group>();
        
    	String n;
        
    	names = new ArrayList<String>();
        //Create a hashmap for name lookups (onClick)
        //Create names list for the ListAdapter to pull from (first+last)
        for(Group g : groups){
        	n = g.getName();
        	names.add(n);
        	hm.put(n, g);
        }
        
        if(groups.isEmpty())
        	setContentView(R.layout.activity_view_no_groups);
        else{
        	setContentView(R.layout.activity_view_groups);
        	setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names));
        }
        
        //Collections.sort(names);
    }
    
    @Override
    protected void onListItemClick(ListView list, View view, int position, long id){
    	super.onListItemClick(list, view, position, id);
    	Group group = hm.get(names.get(position));
    	Intent i = new Intent(this, View_Group.class);
    	i.putExtra("group", group);
    	startActivity(i);
    	
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.view_groups, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_add:
        	addGroup();
        	return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
    private void addGroup(){
    	
    	
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setTitle("Enter name for new group.");
    	final OnTaskCompleted callback = this;
    	// Set up the input
    	final EditText input = new EditText(this);
    	// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
    	input.setInputType(InputType.TYPE_CLASS_TEXT);
    	builder.setView(input);

    	// Set up the buttons
    	builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
    	    @Override
    	    public void onClick(DialogInterface dialog, int which) {
    	        ContactManager.addGroup(new Group(input.getText().toString()));
    	        callback.onTaskComplete();
    	    }
    	});
    	builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    	    @Override
    	    public void onClick(DialogInterface dialog, int which) {
    	        dialog.cancel();
    	    }
    	});

    	builder.show();
    	
    }

	@Override
	public void onTaskComplete() {
		resetGroupsView();
	}
    
}